import { Base } from 'base.js';

class Address extends Base {
  constructor() {
    super();
  }
  
  /*是否为直辖市*/
  isCenterCity(name) {
    var centerCitys = ['北京市', '天津市', '上海市', '重庆市'],
      flag = centerCitys.indexOf(name) >= 0;
    return flag;
  }

  /*
  *根据省市县信息组装地址信息
  * provinceName , province 前者为 微信选择控件返回结果，后者为查询地址时，自己服务器后台返回结果
  */
  setAddressInfo(res) {
    //省
    var province = res.provinceName || res.province,
    //市
      city = res.cityName || res.city,
      //区
      country = res.countyName || res.country,
      detail = res.detailInfo || res.detail;

    var totalDetail = city + country + detail;
    //console.log(res);

    //直辖市，取出省部分
    if (!this.isCenterCity(province)) {
      totalDetail = province + totalDetail;
    };
    return totalDetail;
  }

  //更新保存地址
  submitAddress(data, callback) {
    data = this._setUpAddress(data);
   // console.log(data)
    var param = {
      url: 'getDiZhiAdd',
      type: 'post',
      data: data,
      sCallback: function (res) {
        callback && callback(true, res);
      }, eCallback(res) {
        callback && callback(false, res);
      }
    };
    this.request(param);
  }

  保存地址
  _setUpAddress(res){
    var formData = {
      name:res.userName,
      tel: res.telNumber,
      addres: res.detailInfo,
      province: res.provinceName,
      cityName:res.cityName,
      countyName:res.countyName
    };
    return formData;
  }
}
export { Address }